Imports DotNetNuke
Imports System.Web.UI
Imports System.Collections.Generic
Imports System.Reflection

Namespace CEL.Modules.CELLibrary

    Partial Class ManageBooks
        Inherits Entities.Modules.PortalModuleBase

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Try
                If Not Page.IsPostBack Then
                    Call LocalizeSetting()
                    ViewState("AllowPaging") = "False"
                    Me.lnkShow.Text = "Show 10 records per page"
                    Call LoadLibraryGrid("")
                End If
            Catch exc As Exception        'Module failed to load
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub
        Public Sub LocalizeSetting()
            'Me.lblSearch.Text = DotNetNuke.Services.Localization.Localization.GetString("lblSearch", Me.LocalResourceFile)


        End Sub
        Public Function getViewDetailLink(ByVal Title As String, ByVal LibraryID As Integer) As String
            Return "<a href='" & EditUrl("LibraryID", LibraryID, "ViewBookDetails", "back=vbd") & "'>" & Title & "</a>"
        End Function
        Public Function getBorrowedByname(ByVal OwnerID As Integer) As String
            Dim borrowedByID As Integer = 0
            'Dim objLibraryCon As New CELLibraryController
            'Dim dtTable As DataTable = objLibraryCon.GetBorrowedBookByLibraryID(LibraryID)
            'If dtTable.Rows.Count > 0 Then

            Dim objUserCon As New UserController
            Dim objUserInfo As UserInfo = objUserCon.GetUser(PortalId, OwnerID)
            If Not objUserInfo Is Nothing Then Return objUserInfo.Username
            'End If
            Return ""
        End Function
        Protected Sub dgLibrary_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgLibrary.ItemCommand
            If e.CommandName = "Borrow" Then
                Response.Redirect(EditUrl("LibraryID", Val(e.CommandArgument), "Borrow", "back=vbd"), True)
            ElseIf e.CommandName = "EditBook" Then
                Response.Redirect(EditUrl("LibraryID", Val(e.CommandArgument), "AddBook"), True)
            ElseIf e.CommandName = "Delete" Then
                Dim objLibraryCon As New CELLibraryController
                objLibraryCon.DeleteLibraryByID(Val(e.CommandArgument))
                Call LoadLibraryGrid("")
            ElseIf e.CommandName = "Archive" Then
                Dim objLibraryCon As New CELLibraryController
                objLibraryCon.ArchiveBook(Val(e.CommandArgument))
                Call LoadLibraryGrid("")
            End If

        End Sub
        Protected Sub dgLibrary_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgLibrary.ItemDataBound
            If e.Item.ItemType = ListItemType.Header Or e.Item.ItemType = ListItemType.Footer Then
            Else
                e.Item.Attributes.Add("onmouseover", "this.style.backgroundColor='#ffffcc'")
                e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor='#eeeeee'")

                Dim lnkArchiveIT As LinkButton = CType(e.Item.FindControl("lnkArchive"), LinkButton)
                lnkArchiveIT.Attributes.Add("onclick", "return confirm('Confirm Archive It');")

                Dim lnkDelete As LinkButton = CType(e.Item.FindControl("lnkDelete"), LinkButton)
                lnkDelete.Attributes.Add("onclick", "return confirm('Confirm Delete It');")

                Dim lblReferenceOnly As Label = CType(e.Item.FindControl("lblReferenceOnly"), Label)
                If lblReferenceOnly.Text = "1" Then e.Item.Cells(3).Text = ""
                
            End If
        End Sub
        Public Sub LoadLibraryGrid(ByVal SearchString As String)
            Dim objLibraryCon As New CELLibraryController
            Dim dtTable As DataTable = Nothing
            dtTable = objLibraryCon.getAllNonArchivedLibrary(ModuleId)
            If Convert.ToString(ViewState("AllowPaging")) = "True" Then
                Me.dgLibrary.AllowPaging = True
                Me.dgLibrary.PageSize = 10

            Else
                Me.dgLibrary.AllowPaging = False
            End If
            Me.dgLibrary.DataSource = dtTable
            Me.dgLibrary.DataBind()
        End Sub

        Protected Sub lnkViewArchived_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkViewArchived.Click
            Response.Redirect(EditUrl("Archived"), True)
        End Sub

        Protected Sub lnkBack_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkBack.Click
            Response.Redirect(NavigateURL(TabId), True)
        End Sub

        Protected Sub lnkAddNewButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkAddNewButton.Click
            Response.Redirect(EditUrl("AddBook"), True)
        End Sub

        Protected Sub dgLibrary_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles dgLibrary.PageIndexChanged
            dgLibrary.CurrentPageIndex = e.NewPageIndex
            Call LoadLibraryGrid("")
        End Sub
        '<asp:LinkButton ID="lnkViewBookDetails" CommandArgument='<%#DataBinder.Eval(Container.DataItem,"LibraryID") %>' runat="server" CommandName="ViewBookDetails">View</asp:LinkButton>&nbsp;/&nbsp;

        Protected Sub lnkBorrowedBooks_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkBorrowedBooks.Click
            Response.Redirect(EditUrl("BorrowedBooks"), True)
        End Sub

        Protected Sub lnkShow_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkShow.Click
            If Me.lnkShow.Text = "Show 10 records per page" Then
                ViewState("AllowPaging") = "True"
                'ViewState("PagingSize") = "10"
                Me.lnkShow.Text = "Show all records"
            ElseIf Me.lnkShow.Text = "Show all records" Then
                ViewState("AllowPaging") = "False"
                Me.lnkShow.Text = "Show 10 records per page"
            End If
            Call LoadLibraryGrid("")
        End Sub
    End Class
End Namespace